﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace WpfApplication1.Controls
{
    /// <summary>
    /// Interaction logic for WindowAnnouncement.xaml
    /// </summary>
    public partial class WindowAnnouncement : Window
    {
        private Storyboard RootGridFadeInStoryBoard;
        private Storyboard RootGridFadeOutStoryBoard;
        string _message;
        long _posY;
        long _posX;

        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public long GridHeight
        {
            get { return (long)_Border.ActualHeight; }
        }

        public WindowAnnouncement(string message, long posX, long posY)
        {
            
            _posX = posX;
            _posY = posY;
            InitializeComponent();
            //_posY += (long)_RootGrid.Height;
            
            mainmessage.Text = message;
            RootGridFadeInStoryBoard = (Storyboard)FindResource("_RootGridFadeInStoryBoard");
            RootGridFadeOutStoryBoard = (Storyboard)FindResource("_RootGridFadeOutStoryBoard");
            RootGridFadeInStoryBoard.Completed += new EventHandler((sender, ex) => this.Opacity = 1 );
            RootGridFadeOutStoryBoard.Completed += new EventHandler((sender, ex) =>
                {
                    this.Opacity = 0 ;
                    DoMoveDown((long)_RootGrid.ActualHeight - 10);
                    this.Close();
                });
            this.Left = _posX;
            this.Top = SystemParameters.WorkArea.Height - _posY;
            //ShowWindow();
        }

        public void Show()
        {
            base.Show();
            ShowWindow();
        }

        public void ShowWindow()
        {
            RootGridFadeOutStoryBoard.Stop();
            this.Opacity = 1;
            RootGridFadeInStoryBoard.Begin();
        }

        public void HideWindow()
        {
            RootGridFadeInStoryBoard.Stop();
            RootGridFadeOutStoryBoard.Begin();
        }

        public void MoveDown(long dis)
        {
            DoubleAnimation anima = new DoubleAnimation();
            anima.Duration = new TimeSpan(0, 0, 0, 0, 200);
            anima.From = this.Top;
            anima.To = this.Top + dis;
            anima.FillBehavior = FillBehavior.HoldEnd;
            anima.AutoReverse = false;
            this.BeginAnimation(Window.TopProperty, anima);
        }

        public delegate void MoveDownDelegate(long di);
        public MoveDownDelegate DoMoveDown;
    }
}
